﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： TodoRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-12-11
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using CRM.Infrastructure.Entity;
using Dapper.Extensions;
using CRM.Infrastructure.Models;
using Dapper;
using CRM.Infrastructure.Dto;

namespace CRM.Infrastructure.Repository
{
    /// <summary>
    /// 仓储层
    /// </summary>	
    public class TodoRepository : BaseRepository<Todo>
    {

        #region 构造

        public TodoRepository() : base() { }

        public TodoRepository(string connectionString) : base(connectionString) { }

        public TodoRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion


        public Page<TodoDto> GetPages(TodoSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT T.*,T1.UserName,T1.Sex,T1.CreatedDate AS UserCreatedDate,T1.Model,T1.EndDate,T1.Brand,T1.CarName,T1.HisUserName,T1.Mobile,T1.PlateNo,T1.ClueType FROM Todo T WITH(NOLOCK) ");
            sb.Append("LEFT JOIN Clues T1 WITH(NOLOCK) ON T.ClueID=T1.ClueID  WHERE T.UserID=@UserID");
            var dparams = new DynamicParameters();
            dparams.Add("UserID", model.UserID, System.Data.DbType.Int64);
            AddParams(model, sb, dparams);
            return dbContext.Connection.Page<TodoDto>(model.PageIndex, model.PageSize, sb.ToString(), dparams);
        }

        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(TodoSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (model.TodoDate.Year > 1900)
            {
                sb.Append(" AND T.TodoDateFrt=@TodoDateFrt");
                dparams.Add("TodoDateFrt", model.TodoDate.ToString("yyyyMMdd"), System.Data.DbType.String);
            }
            if (model.IsRead > -1)
            {
                sb.Append(" AND T.IsRead=@IsRead");
                dparams.Add("IsRead", model.IsRead, System.Data.DbType.Int32);
            }
            if (model.FlowStep > -1)
            {
                sb.Append(" AND T.FlowStep=@FlowStep");
                dparams.Add("FlowStep", model.FlowStep, System.Data.DbType.Int32);
            }
            sb.Append(" ORDER BY T.TodoDate ASC");
        }

        /// <summary>
        /// 获取首页统计
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<int> GetStatistic(long userId, DateTime dt)
        {
            string sql = @" SELECT COUNT(TodoID) AS CT FROM Todo WITH(NOLOCK) WHERE UserID=@UserID AND TodoDateFrt=@TodoDateFrt
                            UNION ALL
                            SELECT COUNT(TodoID) AS CT FROM Todo WITH(NOLOCK) WHERE UserID=@UserID AND TodoDateFrt=@TodoDateFrt AND IsRead=0";
            return dbContext.Connection.Query<int>(sql, new { UserID = userId, TodoDateFrt = dt.ToString("yyyyMMdd") }).AsList();
        }

        /// <summary>
        /// 设置代办已读
        /// </summary>
        /// <param name="todoId"></param>
        public void UpdateRead(long todoId)
        {
            string sql = "UPDATE Todo SET IsRead=1 WHERE TodoID=" + todoId;
            Execute(sql);

        }
    }
}